﻿namespace WLFramework.Security
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class SHA1Hash : AlgorithmHash, IChecksum
    {
        public SHA1Hash()
            : base(System.Security.Cryptography.SHA1.Create())
        {
        }

        public override long Value
        {
            get
            {
                return hash == null ? 0 : BitConverter.ToInt64(hash, 8);
            }
        }

        public override string HashCode
        {
            get
            {
                if (hash == null) return string.Empty;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < this.hash.Length; i++)
                {
                    sb.Append(this.hash[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }
    }

    public class SHA256Hash : AlgorithmHash, IChecksum
    {
        public SHA256Hash()
            : base(System.Security.Cryptography.SHA256.Create())
        {
        }

        public override long Value
        {
            get
            {
                return hash == null ? 0 : BitConverter.ToInt64(hash, 8);
            }
        }

        public override string HashCode
        {
            get
            {
                if (hash == null) return string.Empty;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < this.hash.Length; i++)
                {
                    sb.Append(this.hash[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }
    }
    
    public class SHA384Hash : AlgorithmHash, IChecksum
    {
        public SHA384Hash()
            : base(System.Security.Cryptography.SHA384.Create())
        {
        }

        public override long Value
        {
            get
            {
                return hash == null ? 0 : BitConverter.ToInt64(hash, 8);
            }
        }

        public override string HashCode
        {
            get
            {
                if (hash == null) return string.Empty;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < this.hash.Length; i++)
                {
                    sb.Append(this.hash[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }
    }

    public class SHA512Hash : AlgorithmHash, IChecksum
    {
        public SHA512Hash()
            : base(System.Security.Cryptography.SHA512.Create())
        {
        }

        public override long Value
        {
            get
            {
                return hash == null ? 0 : BitConverter.ToInt64(hash, 8);
            }
        }

        public override string HashCode
        {
            get
            {
                if (hash == null) return string.Empty;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < this.hash.Length; i++)
                {
                    sb.Append(this.hash[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }
    }
}
